package com.itmaster.domain.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fernando Perez
 */
public class NewsRepository {

	private static NewsRepository instance = new NewsRepository();
	private final Map<String, List<News>> news = new HashMap<String, List<News>>();;

	/**
	 * Private constructor to avoid instantiation
	 */
	private NewsRepository() {
		initialize();
	}

	/**
	 * Singleton
	 * 
	 * @return {@link NewsRepository}
	 */
	public static NewsRepository getInstance() {
		return instance;
	}

	/**
	 * @param username
	 * @return
	 */
	public List<News> findByUsername(String username) {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		return news.get(username);
	}

	/**
	 * 
	 */
	private void initialize() {
		List<News> newsList = null;

		newsList = new ArrayList<News>();
		for (int i = 0; i < 9; i++) {
			newsList.add(new News("News " + i + " title", "News " + i + " description", "News " + i + " long"));
		}
		news.put(UsersRepository.USERNAME_0, newsList);

		newsList = new ArrayList<News>();
		for (int i = 10; i < 19; i++) {
			newsList.add(new News("News " + i + " title", "News " + i + " description", "News " + i + " long"));
		}
		news.put(UsersRepository.USERNAME_1, newsList);

		newsList = new ArrayList<News>();
		for (int i = 20; i < 29; i++) {
			newsList.add(new News("News " + i + " title", "News " + i + " description", "News " + i + " long"));
		}
		news.put(UsersRepository.USERNAME_2, newsList);
	}

}